package com.hjyoj.judge.codesandbox.impl;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.hjyoj.common.ErrorCode;
import com.hjyoj.exception.BusinessException;
import com.hjyoj.judge.codesandbox.CodeSandBox;
import com.hjyoj.judge.codesandbox.model.ExeccuteCodeRequest;
import com.hjyoj.judge.codesandbox.model.ExeccuteCodeResponse;
import org.apache.commons.lang3.StringUtils;

/**
 * 远程代码沙箱
 */
public class RemoteCodeSandbox implements CodeSandBox {
    //定义权限鉴定
    public static final String AUTH_REQUEST_HEADER = "hjyHeader";
//    public static final String AUTH_REQUEST_HEADER = "auth";
    public static final String AUTH_REQUEST_SECURITY = "securityKey";
//    public static final String AUTH_REQUEST_SECURITY = "secretKey";
    @Override
    public ExeccuteCodeResponse execcuteCode( ExeccuteCodeRequest execcuteCodeRequest) {
        System.out.println("远程代码沙箱");
        String jsonRequest = JSONUtil.toJsonStr(execcuteCodeRequest);
//        String url = "http://192.168.200.129:8089/executeCode";
        String url = "http://8.138.160.219:8089/executeCode";
        String responseStr = HttpUtil.createPost(url)
                .header(AUTH_REQUEST_HEADER,AUTH_REQUEST_SECURITY)
                .body(jsonRequest)
                .execute()
                .body();
        if (StringUtils.isAnyBlank(responseStr)) {
            throw new BusinessException(ErrorCode.API_REQUEST_ERROR, "execcuteCode remoteSandbox error,message= " + responseStr);
        }
        return JSONUtil.toBean(responseStr, ExeccuteCodeResponse.class);
    }
}
